#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
int main()
{
	list<int> l1, l2;
	int n; cin >> n;
	while (n--)
	{
		int tmp; cin >> tmp;
		l1.push_back(tmp);
	}
	cin >> n;
	while (n--)
	{
		int tmp; cin >> tmp;
		l2.push_back(tmp);
	}
	cout << "head-->";
	for (auto it1 = l1.begin(), it2 = l2.begin(); it1 != l1.end() && it2 != l2.end();)
	{
		if (*it1 < *it2)it1++;
		else if (*it1 > *it2)it2++;
		else 
		{
			cout << *it1 << "-->";
			it1++, it2++;
		}
	}
	cout << "tail";
	return 0;
}